package com.ityuwen.mengyagushi.album.listener;

import com.alibaba.fastjson.JSON;
import com.ityuwen.mengyagushi.album.mapper.AlbumStatMapper;
import com.ityuwen.mengyagushi.album.mapper.TrackStatMapper;
import com.ityuwen.mengyagushi.common.constant.KafkaConstant;
import com.ityuwen.mengyagushi.common.constant.RedisConstant;
import com.ityuwen.mengyagushi.common.constant.SystemConstant;
import com.ityuwen.mengyagushi.model.album.AlbumStat;
import com.ityuwen.mengyagushi.model.album.TrackStat;
import com.ityuwen.mengyagushi.vo.album.TrackStatMqVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class TrackListener {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TrackStatMapper trackStatMapper;

    @Autowired
    private AlbumStatMapper albumStatMapper;

    @Transactional
    @KafkaListener(topics = KafkaConstant.QUEUE_TRACK_STAT_UPDATE)
    public void updateStatNum(String json){
        TrackStatMqVo statMqVo = JSON.parseObject(json, TrackStatMqVo.class);
        try {
            if (StringUtils.isBlank(json)){
                return;
            }
            // 获取消息对象
            Boolean flag = this.redisTemplate.opsForValue().setIfAbsent(RedisConstant.ALBUM_TRACK_STAT_PREFIX + statMqVo.getBusinessNo(), "");
            if (!flag){ // 如果重复消费
                return;
            }

            // 更新声音的统计信息
            TrackStat trackStat = this.trackStatMapper.selectOne(new LambdaQueryWrapper<TrackStat>()
                    .eq(TrackStat::getTrackId, statMqVo.getTrackId())
                    .eq(TrackStat::getStatType, statMqVo.getStatType()));
            trackStat.setStatNum(trackStat.getStatNum() + statMqVo.getCount());
            this.trackStatMapper.updateById(trackStat);

            // 判断声音的统计维度是否是播放量和评论数
            if (SystemConstant.TRACK_STAT_PLAY.equals(statMqVo.getStatType())
                    || SystemConstant.TRACK_STAT_COMMENT.equals(statMqVo.getStatType())){
                String statType = null;
                switch (statMqVo.getStatType()){
                    case SystemConstant.TRACK_STAT_PLAY -> statType = SystemConstant.ALBUM_STAT_PLAY;
                    case SystemConstant.TRACK_STAT_COMMENT -> statType = SystemConstant.ALBUM_STAT_COMMENT;
                }
                // 更新专辑的统计信息
                AlbumStat albumStat = this.albumStatMapper.selectOne(new LambdaQueryWrapper<AlbumStat>()
                        .eq(AlbumStat::getAlbumId, statMqVo.getAlbumId())
                        .eq(AlbumStat::getStatType, statType));
                albumStat.setStatNum(albumStat.getStatNum() + statMqVo.getCount());
                this.albumStatMapper.updateById(albumStat);
            }
        } catch (Exception e) {
            // 如果程序异常则删除防重的唯一标识，进行重试
            this.redisTemplate.delete(RedisConstant.ALBUM_TRACK_STAT_PREFIX + statMqVo.getBusinessNo());
            throw new RuntimeException(e);
        }
    }
}
